$(document).ready(function(){
	freshRequestList();
});

function changeList(){}

function pushChangeStatus(requestId, statusName) {
	var status = "";
	if (statusName == "authed") {
		status = 2;		
		$("#" + requestId).replaceWith('	<img id="' + requestId + '" class="pointer fade_effect" src="../image/btn_authorized.png" align="top" style="margin-left: 15px;" onclick="pushChangeStatus(\'' + requestId + '\', \'forbidden\');"/>');
	} else {
		status = 1;
		$("#" + requestId).replaceWith('	<img id="' + requestId + '" class="pointer fade_effect" src="../image/btn_forbidden.png" align="top" style="margin-left: 15px;" onclick="pushChangeStatus(\'' + requestId + '\', \'authed\');"/>');
	}
	changeList["id_" + requestId] = status;
}

function getStatuStr() {
	var ret = "";
	for (var key in changeList) {
		if (ret != "") {
			ret += ",";
		}
		ret += key.replace("id_", "") + ":" + changeList[key];
	}
	
	return ret;
}

function saveChangedStatus() {
	var requestUrl = "merchant_json_servlet";
	var statusStr = getStatuStr();
		
	if (statusStr == "") {
		showMessage("状态未改变");
		return;
	}

	var params = {
			request_method:"place_requests_authorization_batch",
			request_ids:statusStr
	};
	
	$.post(requestUrl, params, function(ret) {
		showMessage(ret.message);
	}, "json");
}

function freshRequestList() {
	var requestUrl = "get_place_requests";
	var requestHtml = "";
	
	$.post(requestUrl, "", function(data){
		if (data.code != 0) {return;}
		
		for (var i = 0; data.resultData && i < data.resultData.length; i++) {
			var place = data.resultData[i];
			
			if ( i > 0) {
				requestHtml += '<div style="margin: 10px 5px; margin-top: 25px; border-bottom: 1px dashed gray;"></div>';
			}
			
			requestHtml += '<div style="padding-left: 20px; padding-top: 10px; font-size: 13pt; font-weight: bold;">';
			requestHtml += '	自主经营场所：' + place.placeName;
			requestHtml += '</div>';
			for(var j = 0; place.requests && j < place.requests.length; j++) {
				var request = place.requests[j];
				
				requestHtml += '<div style="padding-left: 40px; padding-top: 15px;">';
				requestHtml += '	<div style="width: 340px; height: 30px; display: inline-block; background: url(\'../image/bg_place_name.png\') no-repeat;">';
				requestHtml += request.corporationName;
				requestHtml += '	</div>';
				if (request.requestStatus != 2) {
					requestHtml += '	<img id="' + request.requestId + '" class="pointer fade_effect" src="../image/btn_forbidden.png" align="top" style="margin-left: 15px;" onclick="pushChangeStatus(\'' + request.requestId + '\', \'authed\');"/>';
				} else {
					requestHtml += '	<img id="' + request.requestId + '" class="pointer fade_effect" src="../image/btn_authorized.png" align="top" style="margin-left: 15px;" onclick="pushChangeStatus(\'' + request.requestId + '\', \'forbidden\');"/>';
				}
				requestHtml += '</div>';
			}
		}
		
		$("#div_place_requests").html(requestHtml);
		addFadeEffect();
	}, "json");
}